﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Text.RegularExpressions;

using System.IO;
using MINERP.MODEL.NewPinBan;
using MINERP.COM;
using MINERP.NewPB.Presenter;

namespace MINERP.NewPB.Forms
{
    public partial class FrmSelectDXF : Form
    {
        public FrmSelectDXF(PhotoInfo_PinBan inPbdctPbdct)
        {
            InitializeComponent();
            _photoInfoPbdct = inPbdctPbdct;
            _currPath = inPbdctPbdct.PicPath;   
        }
        PhotoInfo_PinBan _photoInfoPbdct = new PhotoInfo_PinBan();
        private string _currPath { get; set; }
        private List<string> FileNamelist = new List<string>();
        public string SelectPath { get; set; }
        private void FrmSelectDXF_Load(object sender, EventArgs e)
        {
            try
            {
                //WriteLogForCheck.WriteLogFile("获取到" + IniFile.DxfFolderPath);

                if (string.IsNullOrEmpty(Properties.Settings.Default.DxfFolderPath))
                {
                    return;
                }

                var FileNamelist = DXFHelper.GetDxfFile(Properties.Settings.Default.DxfFolderPath);
                // WriteLogForCheck.WriteLogFile("获取到"+FileNamelist.Count());
                //GetDxfFile(IniFile.DxfFolderPath);

                BindDXFList(FileNamelist);
                labelControlInfo.Text = _photoInfoPbdct.PicPath;
                if (FileNamelist.Count > 0)
                {
                    listBoxControldxfList.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {

                WriteLogForCheck.WriteLogFile(ex.ToString());
            }
        }
        private void BindDXFList(List<string> FileNamelist)
        {
            if (FileNamelist.Count > 0)
            {

                FileNamelist = DXFHelper.OrderDxfItem(FileNamelist, _photoInfoPbdct.PicPath);
            }
            listBoxControldxfList.Items.Clear();
            FileNamelist.ForEach(ea =>
            {
                listBoxControldxfList.Items.Add(ea.ToString());
            });
            DataTransfer.DxfList = FileNamelist;
        }
    

        private void listBoxControldxfList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            GetSelectItem();
        }
        private void GetSelectItem()
        {
            var selectItem = listBoxControldxfList.SelectedItem;
            if (selectItem != null)
            {
                string slStr = selectItem.ToString();
                if (!"清除".Equals(slStr))
                {
                    SelectPath = slStr;
                    byte[] data;
                    using (FileStream fs = File.OpenRead(SelectPath))
                    {
                        data = new byte[fs.Length];
                        fs.Read(data, 0, data.Length);
                    }
                    _photoInfoPbdct.DXFContent = data;
                    _photoInfoPbdct.DXFPath = SelectPath;
                }
                else
                {
                    _photoInfoPbdct.DXFContent = new byte[1];
                    _photoInfoPbdct.DXFPath = "";
                }
                this.DialogResult = DialogResult.OK;
            }
            this.Close();
        }

        private void listBoxControldxfList_DoubleClick(object sender, EventArgs e)
        {
            GetSelectItem();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonEditSelectPath_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void buttonEditSelectPath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.SelectedPath = Properties.Settings.Default.DxfFolderPath;
            folderBrowserDialog.Description = "选择DXF文件夹";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.DxfFolderPath = folderBrowserDialog.SelectedPath;
                Properties.Settings.Default.Save();
            }
            this.buttonEditSelectPath.Text = Properties.Settings.Default.DxfFolderPath;
            FileNamelist = GetDxfFile(Properties.Settings.Default.DxfFolderPath);
            BindDXFList(FileNamelist);
        }
        private List<string> GetDxfFile(string inPath)
        {
            List<string> rtnList = new List<string>();
            if (System.IO.Directory.Exists(inPath))
            {
                rtnList = System.IO.Directory.GetFiles(inPath, "*.dxf").ToList();
            }
            else
            {
                return new List<string>();
            }
            return rtnList;
        }
    }
}
